<template>
	<view>
		<view class="uni-header">
			<uni-stat-breadcrumb class="uni-stat-breadcrumb-on-phone" />
		</view>
		<view class="uni-container">
			<view class="clearfix">
				<view style="display: inline-block;margin-right: 10px;">【 显示在线人线 】</view>
				<view class="" @click="getPlayerList" style="cursor: pointer;color: #00aaff;">
					刷新
				</view>
			</view>

			<view class="item_data" v-for="(item,key) in tableData"
				style="cursor: pointer;padding: 10px;border-top: #00aaff solid 2px;font-size: 16px;">
				<view class="top" style="display: flex;flex-wrap: wrap;align-items: center;gap: 5px;color: #919191;">
					<text
						style="width: 20px;height: 20px;border-radius: 50%;line-height: 20px;display: inline-block;background-color: #00aaff;text-align: center;color: #fff;">{{item.num}}</text>
					科雷id
					<text style="color: #00aaff;margin-left: 10px;">{{item.name}}</text>
					steam名称
					<text style="color: #aa55ff;margin-left: 10px;">{{item.steamName}}</text>
					角色
					<text style="color: #aa007f;margin-left: 10px;">{{item.role}}</text>
				</view>

				<view class="endButn" style="display: flex;flex-wrap: wrap;align-items: center;gap: 5px;margin-top: 10px;">
					<view @click="addAdmin(dataName)">加管理员</view>
					<view @click="addWhite(dataName)">加白名单</view>
					<view @click="addBlack(dataName)">加黑名单</view>
					<view @click="kickPlayer(dataName)">踢出玩家</view>
					<view @click="killPlayer(dataName)">杀死玩家</view>
					<view @click="resurgencePlayer(dataName)">复活玩家</view>
					<view @click="despawnPlayer(dataName)">更换人物</view>
					<view @click="createPlayer(dataName)">创造模式</view>
					<view @click="godmodePlayer(dataName)">上帝模式</view>
				</view>
			</view>

			<view class="el-card is-always-shadow is-always-shadow usertextarea"
				style="box-sizing: border-box;padding: 20px;display: flex;justify-content: space-around;width: 100%;margin-top: 10px;flex-direction: row;flex-wrap: wrap;gap: 20rpx;">
				<view class="" style="flex: 1;text-align: center;min-width: 320px;">
					<view class="demonstration" style="margin-bottom: 5px;">【管理员列表】</view>
					<textarea placeholder="请输入冒险家科雷id" v-model="adminData" auto-height />
				</view>
				<view class="" style="flex: 1;margin: 0 1%;text-align: center;min-width: 320px;">
					<view style="margin-bottom: 5px;">【白名单列表】</view>
					<textarea placeholder="请输入冒险家科雷id" v-model="whiteData" auto-height />
				</view>
				<view class="" style="flex: 1;text-align: center;min-width: 320px;">
					<view class="demonstration" style="margin-bottom: 5px;">【黑名单列表】</view>
					<textarea placeholder="请输入冒险家科雷id" v-model="blackData" auto-height />
				</view>
			</view>
			<view style="margin: 10px; position: sticky; bottom: 0;  z-index: 10;gap: 20px;display: flex;">
				<button size="mini" type="primary" @click="saveAdminAndBlackList()">
					保存
				</button>
				<button size="mini" type="primary" @click="saveAndRestart()">
					保存重启生效
				</button>
				<view class="" style="flex: 1;">
					<!-- 占位 -->
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				activeName: 'first',
				adminList: [],
				blackList: [],
				whiteList: [],
				playerList: [],
				tableData: [],
				adminData: null,
				blackData: null,
				whiteData: null,
				dataName: null
			}
		},
		created() {
			this.init();
		},
		methods: {

			init() {
				this.adminList = []
				this.blackList = []
				this.whiteList = []
				this.$http.get("/player/getDstAdminList").then((data) => {
					if (data) {
						this.adminList = data.data;
						this.adminList.forEach((item, index) => {
							if (item) {
								this.adminData += item
							}
							if (this.adminList.length - 1 != index) {
								this.adminData += "\n"
							}
						})
					}
				})
				this.$http.get("/player/getDstBlacklist").then((data) => {
					if (data) {
						this.blackList = data.data;
						this.blackList.forEach((item, index) => {
							if (item) {
								this.blackData += item
							}
							if (this.blackList.length - 1 != index) {
								this.blackData += "\n"
							}
						})
					}
				})
				this.$http.get("/player/getDstWhitelist").then((data) => {
					if (data) {
						this.whiteList = data.data;
						this.whiteList.forEach((item, index) => {
							if (item) {
								this.whiteData += item
							}
							if (this.whiteList.length - 1 != index) {
								this.whiteData += "\n"
							}
						})
					}
				})
				this.getPlayerList();
			},
			//保存并重启
			saveAndRestart() {
				this.saveAdminAndBlackList();
				this.restart();
			},
			saveAdminAndBlackList() {
				let param = {};
				this.adminList = []
				this.blackList = []
				this.whiteList = []
				this.adminData.split("\n").forEach(item => {
					if (item) {
						this.adminList.push(item)
					}
				})

				this.blackData.split("\n").forEach(item => {
					if (item) {
						this.blackList.push(item)
					}
				})
				this.whiteData.split("\n").forEach(item => {
					if (item) {
						this.whiteList.push(item)
					}
				})
				param.adminList = this.adminList;
				param.blackList = this.blackList;
				param.whiteList = this.whiteList;
				this.$http.post("/player/saveAdminAndBlackList", param).then((data) => {
					if (!data) {
						this.$message({
							message: data.message,
							type: 'warning'
						});
					} else {
						this.$message({
							message: '保存成功',
							type: 'success'
						});
						this.init();
					}
				})
			},
			restart() {
				this.$http.get("/home/stop?type=0").then((data) => {})
				this.$http.get("/home/start?type=0").then((data) => {})
			},
			getPlayerList() {
				this.$http.get("/home/getPlayerList").then((data) => {
					if (data) {
						this.tableData = []
						this.playerList = data.data;
						data.data.forEach((item, index) => {
							let split = item.split(" ")
							let split1 = ''
							if (split[2] == "wormwood	") {
								split1 += "沃姆伍德(植物人)"
							} else if (split[2] == "wilson	") {
								split1 += "威尔逊"
							} else if (split[2] == "willow	") {
								split1 += "微洛(火女)"
							} else if (split[2] == "wolfgang	") {
								split1 += "沃尔夫冈(大力士)"
							} else if (split[2] == "wendy	") {
								split1 += "温蒂(温武神)"
							} else if (split[2] == "wx78	") {
								split1 += "WX-78(机器人)"
							} else if (split[2] == "wickerbottom	") {
								split1 += "微克巴顿(老奶奶)"
							} else if (split[2] == "woodie	") {
								split1 += "伍迪"
							} else if (split[2] == "wes	") {
								split1 += "韦斯"
							} else if (split[2] == "waxwell	") {
								split1 += "麦斯威尔"
							} else if (split[2] == "wathgrithr	") {
								split1 += "维格弗德(女武神)"
							} else if (split[2] == "webber	") {
								split1 += "韦伯(蜘蛛人)"
							} else if (split[2] == "winona	") {
								split1 += "微诺娜(女工)"
							} else if (split[2] == "warly	") {
								split1 += "沃利(厨师)"
							} else if (split[2] == "wortox	") {
								split1 += "沃拓克斯(恶魔人)"
							} else if (split[2] == "walter	") {
								split1 += "沃尔特"
							} else if (split[2] == "wanda	") {
								split1 += "旺达"
							} else if (split[2] == "wurt	") {
								split1 += "沃特"
							} else {
								split1 += "未知"
							}
							this.tableData.push({
								'num': index + 1,
								'name': split[0],
								'steamName': split[1],
								'role': split1
							})
						})
					}
				})
			},
			addAdmin(val) {

				if (this.adminData.includes(val)) {
					this.$message({
						message: '已在列表内',
						type: 'warning'
					});
				} else {
					if (this.adminData) {
						this.adminData += "\n"
					}
					this.adminData += val;
					this.$message({
						message: '设置成功请保存!',
						type: 'success'
					});
				}
			},
			addWhite(val) {
				if (this.whiteData.includes(val)) {
					this.$message({
						message: '已在列表内',
						type: 'warning'
					});
				} else {
					if (this.whiteData) {
						this.whiteData += "\n"
					}
					this.whiteData += val;
					this.$message({
						message: '设置成功请保存!',
						type: 'success'
					});
				}
			},
			addBlack(val) {
				if (this.blackData.includes(val)) {
					this.$message({
						message: '已在列表内',
						type: 'warning'
					});
				} else {
					if (this.blackData) {
						this.blackData += "\n"
					}
					this.blackData += val;
					this.$message({
						message: '设置成功请保存!',
						type: 'success'
					});
				}
			},
			//踢出玩家
			kickPlayer(name) {
				this.$http.get("/home/kickPlayer?userId=" + name).then((data) => {
					if (data.code != 0) {
						this.warningMessage(data.message);
					}
					this.successMessage('执行成功');
				})
				this.getPlayerList();
			},
			killPlayer(name) {
				this.$http.get("/player/killPlayer?userId=" + name).then((data) => {
					if (data.code != 0) {
						this.warningMessage(data.message);
					}
					this.successMessage('执行成功');
				})
				this.getPlayerList();
			},
			resurgencePlayer(name) {
				this.$http.get("/player/resurgencePlayer?userId=" + name).then((data) => {
					if (data.code != 0) {
						this.warningMessage(data.message);
					}
					this.successMessage('执行成功');
				})
				this.getPlayerList();
			},
			despawnPlayer(name) {
				this.$http.get("/player/despawnPlayer?userId=" + name).then((data) => {
					if (data.code != 0) {
						this.warningMessage(data.message);
					}
					this.successMessage('执行成功');
				})
				this.getPlayerList();
			},
			createPlayer(name) {
				this.$http.get("/player/createPlayer?userId=" + name).then((data) => {
					if (data.code != 0) {
						this.warningMessage(data.message);
					}
					this.successMessage('执行成功');
				})
				this.getPlayerList();
			},
			godmodePlayer(name) {
				this.$http.get("/player/godmodePlayer?userId=" + name).then((data) => {
					if (data.code != 0) {
						this.warningMessage(data.message);
					}
					this.successMessage('执行成功');
				})
				this.getPlayerList();
			},
			successMessage(e){
				this.$message({
					message: e,
					type: 'success'
				});
			}
			
		}
	}
</script>

<style scoped>
	.uni-container {
		font-size: 10px;
	}

	.clearfix {
		padding: 10px;
		font-size: 400;
		font-size: 14px;
		line-height: 40px;
		height: 40px;
		align-items: center;
		display: flex;
		justify-content: flex-start;
	}

	.usertextarea>view>textarea {
		width: 90%;
		min-height: 80px;
		padding: 10px;
		border: 1px solid #ddd;
		margin: auto;
		font-size: 12px;
	}
	.endButn>view{
		padding: 5px;
		border: #919191 1px solid;
		border-radius: 6px;
		font-size: 12px;
		color: #355;
		/* background-color: #355; */
	}
	.endButn>view:hover{
		padding: 5px;
		border: #919191 1px solid;
		border-radius: 6px;
		font-size: 12px;
		color: #ddd;
		background-color: #355;
	}
</style>